package com.guang.syncronized.review;

import org.openjdk.jol.info.ClassLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @Description 默认不开启偏向延迟。在进入到同步块中去会升级成轻量级锁
 *              证明：在轻量级锁的情况下，让线程互斥执行，依然是轻量级锁
 * @Author liguang
 * @Date 2022/10/22/08:27
 */
public class SyncronziedReviewTestOne {
    private final static Logger logger = LoggerFactory.getLogger(SyncronziedReviewTestOne.class);
    static Object lock = new Object();
    public static void main(String[] args) throws InterruptedException {
        logger.info("主线程启动的时候，JVM默认开启不可偏向");
        logger.info("打印当前对象的对象头，{}",ClassLayout.parseInstance(lock).toPrintable());
        new Thread(()->{
            synchronized (lock){
                logger.info("因为不可偏向，然后又来加锁，应该是轻量级锁：000");
                logger.info("t1线程打印当前对象的对象头，{}",ClassLayout.parseInstance(lock).toPrintable());
            }
        },"t1").start();
        TimeUnit.SECONDS.sleep(10);
        new Thread(()->{
            synchronized (lock){
                logger.info("轻量级锁。000，没有发生线程并发，所以还是000，只不过lock record变化了");
                logger.info("t2线程打印当前对象的对象头，{}",ClassLayout.parseInstance(lock).toPrintable());
                logger.info("难道是因为发生了锁的撤销导致当前不是000？而是001");
            }
        },"t2").start();

    }
}
